﻿var currentPage = 1; // 当前页
var pageSize = 50; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数

// 读取列表
$(document).ready(function() {    
    showList(currentPage);
});

// 条件查询资源列表
function showList(currentPage) {
    var customerName = LexunJS.GetQueryString("customerName");          // 客户名称
    var customerTel = LexunJS.GetQueryString("customertel");            // 客户电话    
    var room = LexunJS.GetQueryString("room");                          // 房
    var hall = LexunJS.GetQueryString("hall");                          // 厅
    var price1 = LexunJS.GetQueryString("price1");                      // 租售价
    var price2 = LexunJS.GetQueryString("price2");                      // 租售价
    var customerClass = LexunJS.GetQueryString("customerClass");        // 客户等级 如:普通\实在...
    var tradeType = LexunJS.GetQueryString("tradeType");                // 交易类型 如:求购、求租
    var dname = LexunJS.GetQueryString("dname");                        // 城区名称
    var aname = LexunJS.GetQueryString("aname");                        // 片区名称
    var acreage1 = LexunJS.GetQueryString("acreage1");                  // 面积1
    var acreage2 = LexunJS.GetQueryString("acreage2");                  // 面积2
    var infoFrom = LexunJS.GetQueryString("infoFrom");                  // 来源
    

    // 高级查询
    var id = LexunJS.GetQueryString("id");                              // 序号
    var fitment = LexunJS.GetQueryString("fitment");                    // 装修
    var floor1 = LexunJS.GetQueryString("floor1");                      // 楼层
    var floor2 = LexunJS.GetQueryString("floor2");                      // 楼层
    var addDate1 = LexunJS.GetQueryString("addDate1");                  // 委托日期
    var addDate2 = LexunJS.GetQueryString("addDate2");                  // 委托日期
    var usage = LexunJS.GetQueryString("usage");                        // 用途
    var houseStyle = LexunJS.GetQueryString("houseStyle");              // 样式
    var direct = LexunJS.GetQueryString("direct");                      // 朝向
    var type = LexunJS.GetQueryString("type");                          // 类型
    var status = LexunJS.GetQueryString("status");                      // 状态 0当前 1资料
    var isSelf = LexunJS.GetQueryString("isSelf");                      // 性质 -1全部 0私客 1公客
    var isChange = LexunJS.GetQueryString("isChange");                  // 0非转介客 1转介客
    var estates = LexunJS.GetQueryString("estates");                    // 意向楼盘名称
    var ownerDept = LexunJS.GetQueryString("ownerDept");                // 所属人部门
    var owner = LexunJS.GetQueryString("owner");                        // 所属人

    var ischangeout = LexunJS.GetQueryString("ischangeout");            //是包含转出客
    var lookCount1 = LexunJS.GetQueryString("lookCount1");              //看房次数
    var lookCount2 = LexunJS.GetQueryString("lookCount2");              //看房次数

    // 排序字段
    var orderByName = $("#hOrderByName").val();
    var orderByType = $("#hOrderByType").val();

    var remark = LexunJS.GetQueryString("remark");


    $("#divList").html("<img src=\"../images/loading.gif\" />");        // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=customer_list&typeid=1&page=' + currentPage + '&pagesize=' + pageSize + '&customerName=' + customerName + '&customertel=' + customerTel
                    + '&room=' + room + '&hall=' + hall + '&price1=' + price1 + '&price2=' + price2 + '&customerClass=' + customerClass + '&tradeType=' + tradeType
                    + '&dname=' + dname + '&aname=' + aname + '&acreage1=' + acreage1 + '&acreage2=' + acreage2
                    + '&id=' + id + '&fitment=' + fitment + '&floor1=' + floor1 + '&floor2=' + floor2 + '&addDate1=' + addDate1 + '&addDate2=' + addDate2 + '&usage=' + usage
                    + '&houseStyle=' + houseStyle + '&direct=' + direct + '&type=' + type + '&status=' + status + '&isSelf=' + isSelf + '&isChange=' + isChange + "&estates=" + estates
                    + '&ownerDept=' + escape(ownerDept) + '&owner=' + escape(owner)
                    + '&orderByName=' + orderByName + '&orderByType=' + orderByType + '&infoFrom=' + infoFrom + '&lookCount1=' + lookCount1 + '&lookCount2=' + lookCount2 + "&ischangeout=" + ischangeout+ "&remark=" + escape(remark),
        timeout: '20000',
        error: function() {
            alert("系统繁忙,请稍候后试！");
        }, success: function(json) {
            listCallBack(json);
        }
    });
}

/*
功能: 将返回的json数据进行排版
json: json数据
*/
function listCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;

    var arr = [];
    arr.push("<table id='tableList' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr class='fixHeaderStyle'>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");    
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('tradetype')\" >交易</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('status')\" >状态</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('customer')\" >姓名</a>");  
    arr.push("</td>");    
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('remark')\" >备注</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('isself')\" >类型</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('customerclass')\" >等级</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('mark2')\" >看房次数</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('owner')\" >接待人</a>");
    arr.push("</td>");    
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('ownerdept')\" >接待部门</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('followupdate')\" >上次跟进</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('ownerdate')\" >接待日期</a>");
    arr.push("</td>");    
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('dname')\" >城区</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('area')\" >片区</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('')\" >客源编号</a>");
    arr.push("</td>");
    
    // 权限判断
    var hCompetencesIsEdit = $("#hCompetencesIsEdit").val();    // 0不允许修改 1允许修改
    if (hCompetencesIsEdit == 1) {
        arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
        arr.push("基本操作");
        arr.push("</td>");
    }
    arr.push("</tr>");

    var id_list = "";   // 保存查询出客源的ID 如 1211,1212,1213...
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        id_list += record[i].id;
        if (i < recordLength - 1)
            id_list += ",";

        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");        
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].tradetype);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].status == 0)
            arr.push("当前");
        else if (record[i].status == 1)
            arr.push("资料");        
        arr.push("</td>");        
        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].customer + "</a>");
        arr.push("<td height='20' width='220px' title='" + record[i].remark + "'>");
        if (record[i].remark.length > 20)
            arr.push(record[i].remark.substring(0, 20) + "...");
        else
            arr.push(record[i].remark);
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].isself == 0)
            arr.push("私客");
        else if (record[i].isself == 1)
            arr.push("公客");
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].customerclass);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].mark2);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].owner);
        arr.push("</td>");        
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].ownerdept);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].followupdate);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].ownerdate);
        arr.push("</td>");                
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].dname);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].area);
        arr.push("</td>");              
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].id);
        arr.push("</td>");
        arr.push("<td height='20'  class='STYLE6'>"); //操作：修改 --本人可以修改自己的客源，分行经理修改本组客源，系统管理员修改全部客源
        arr.push("<a href='javascript:detail(" + record[i].id + ","+record[i].mark1+")'>修改</a> ");
        arr.push("</td>");

        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表
    $("#divPage").html(""); // 清空

    var pageHtml = LexunJS.showPage(currentPage, pageall, "showList"); // 分页
    $("#divPage").html(pageHtml);
        
    $("#hID_List").val(id_list);
}

// 响应键盘查询
function responseEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13)
        search();
}

// 条件查询
function search() {
    $("#divList").html(""); // 清空列表
    $("#divPage").html(""); // 清空分页

    showList(1);
}

// 按字段排序
function searchOrderBy(orderName) {
    $("#divList").html("");     // 清空列表
    $("#divPage").html("");     // 清空分页

    $("#hOrderByName").val(orderName);     // 保存排序字段
    var orderByType = $("#hOrderByType").val();
    if (orderByType == "asc")
        $("#hOrderByType").val("desc");
    else
        $("#hOrderByType").val("asc");

    showList(1);
}

// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        LexunJS.addOrCutData(1, "hID", objvalue);
    }
    else {
        LexunJS.addOrCutData(0, "hID", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("hID").value = "";
    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "hID", checkList[i].id);
            }
        }
    }
    else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "hID", checkList[i].id);
            }
        }
    }
}

// 修改
function detail(id, checkStatus) {
    if (checkStatus == "1") {
        alert("待审客源，不允许修改！");
        return;
    }
     if (id <= 0) {
         var IDList = document.getElementById("hID").value;
         var idlist = IDList.split('|');
         var idleng = idlist.length - 1;
         id = idlist[0];
         if (idleng > 1) {
             alert("只能选择一行！");
             return false;
         }
         if (id == "") {
             alert("请先选择您要修改的客源！");
             return false;
         }
         document.getElementById("hID").value = "";
     }
     var url = "../CM/Customer.aspx?id=" + id;

    self.parent.closeTab('修改客源');
    self.parent.addTab('修改客源', url, 'icon-nav');
}


function detail1(id) {
    if (id <= 0) {
        var IDList = document.getElementById("hID").value;
        var idlist = IDList.split('|');
        var idleng = idlist.length - 1;
        id = idlist[0];
        if (idleng > 1) {
            alert("只能选择一行！");
            return false;
        }
        if (id == "") {
            alert("请先选择您要修改的客源！");
            return false;
        }
        document.getElementById("hID").value = "";
    }
    var url = "../CM/Customer.aspx?id=" + id;

    self.parent.closeTab('修改客源');
    self.parent.addTab('修改客源', url, 'icon-nav');
    return false;
}

// 删除
function del() {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要删除的行！");
        return false;
    }

    if (confirm("您确定要删除该行，请谨慎操作！")) {
        // ajax删除                   
        $.ajax({
            type: 'POST',
            url: '../Handler/HandlerCustomer.ashx',
            data: 'act=customer_del&id=' + IDList,
            timeout: '10000',
            error: function() {
                alert("删除失败！");
            }, success: function(outmsg) {
                $("#hID").val("");
                alert(outmsg);
                showList(currentPage);
            }
        });
    }

    return false;
}

// 转私客 转公客 isself 0私客 1公客
function change(isself) {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    if (confirm("您确定要操作该行，请谨慎操作！")) {
        $.ajax({
            type: 'POST',
            url: '../Handler/HandlerCustomer.ashx',
            data: 'act=customer_changeself&id=' + IDList,
            timeout: '10000',
            error: function() {
                alert("操作失败！");
            }, success: function(outmsg) {
                $("#hID").val("");
                alert(outmsg);
                showList(currentPage);
            }
        });
    }
    
    return false;
}

///转公客
function ChangePublicCustomer(isself) {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    if (confirm("您确定要操作该行，请谨慎操作！")) {
        $.ajax({
            type: 'POST',
            url: '../Handler/HandlerCustomer.ashx',
            data: 'act=customer_change&id=' + IDList + "&isself=" + isself,
            timeout: '10000',
            error: function() {
                alert("操作失败！");
            }, success: function(outmsg) {
                $("#hID").val("");
                alert(outmsg);
                showList(currentPage);
            }
        });
    }

    

    return false;
}


// 转介客
function changeTo() {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    var path = "CustomerChange.aspx?idlist=" + IDList;
    var parameter = "确认接收人";
    var style = "dialogWidth:450px; dialogHeight:200px; status:0;scroll:1; help:0; resizable:1;Scrollbars=yes,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    window.showModalDialog(path, parameter, style);
    window.location.reload();

    return false;
}

// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");
}

// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../CM/CustomerDetail.aspx?id=" + id + "&id_list=" + escape(id_list);
    var style = "left = 50, screenX = 0, top = 50, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=450px,Toolbar=no,Location=no,Direction=no,Resizeable=no";


    //self.parent.closeTab('客源详情');
    //self.parent.addTab('客源详情', url, 'icon-nav');
    window.open(url, '', style);

    //window.open("CustomerDetail.aspx?id=" + id + "&id_list=" + escape(id_list), "target", LexunJS.openParams());
}


function ShowCustomerFloow() {
    var IDlist = document.getElementById("hID").value;
    var str = new Array();
    str = IDlist.split('|');

    var id = str[0];
    if (id == "") {
        alert("请选择需要操作的行！");
        return false;
    }
    document.getElementById("hID").value = ""; 
    var url = "../CM/CustomerFloow.aspx?id=" + id;
    var style = "left = 50, screenX = 0, top = 50, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=450px,Toolbar=no,Location=no,Direction=no,Resizeable=no";

    window.open(url, '', style);

    return false;
 }